﻿#define _CRT_SECURE_NO_WARNINGS 1

// 字符串相关函数实现

// strcpy 的模拟实现
#include <stdio.h>
#include <assert.h>
void my_strcpy(char* destination, const char* source)
{
	assert(destination);
	assert(source);
	while (*source != '\0')
	{
		*destination++ = *source++;
	}
	*destination = '\0';
}

void my_strcpy(char* destination, const char* source)
{
	assert(destination);
	assert(source);
	while (*destination++ = *source++);
}

char* my_strcpy(char* destination, const char* source)
{
	assert(destination);
	assert(source);
	char* pmove = destination;
	while (*pmove++ = *source++);
	return destination;
}
int main()
{
	char arr1[] = "abcdef";
	char arr2[20] = { 0 };
	my_strcpy(arr2, arr1);
	printf("%s\n", arr2);
	return 0;
}
 
 
  // strncpy 的使用
#include <stdio.h>
#include <string.h>
int main()
{
	char arr1[] = "abcdef";
	char arr2[20] = { 0 };
	strncpy(arr2, arr1, sizeof(char)*strlen(arr1)-1);
	printf("%s\n", arr2);
	return 0;
}

// strcat 的模拟实现
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "Hello ";
	char arr2[] = "World!";
	strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

#include <stdio.h>

char* my_strcat(char* destination, const char* source)
{
	assert(destination && source);
	char* pmove = destination;
	while (*pmove != '\0')
		pmove++;
	while (*pmove++ = *source++);
	return destination;
}
int main()
{
	char arr1[20] = "Hello ";
	char arr2[] = "World!";
	my_strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

// strcat 易错点（自己给自己追加）
#include <stdio.h>

char* my_strcat(char* destination, const char* source)
{
	assert(destination && source);
	char* pmove = destination;
	while (*pmove != '\0')
		pmove++;
	while (*pmove++ = *source++);
	return destination;
}
int main()
{
	char arr1[20] = "Hello ";
	my_strcat(arr1, arr1);
	printf("%s\n", arr1);
	return 0;
}


 strncat 的使用

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "Hello ";
	strncat(arr1, arr1, strlen(arr1)*sizeof(char));
	printf("%s\n", arr1);
	return 0;
}

// strncat 的模拟实现
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char* my_strncat(char* destination, const char* source, size_t num)
{
	if (destination == source)
	{
		char* pmove = destination;
		int len = (int)strlen(destination);
		char* temp = (char*)malloc(sizeof(char) * (len + 1));
		strcpy(temp, destination);
		while (*pmove != '\0')
			pmove++;
		while (*temp != '\0' && num > 0)
		{
			*pmove = *temp;
			pmove++;
			temp++;
			num--;
		}
		*pmove = '\0';
	}
	else
	{
		char* pmove = destination;

		// 将指针移动到目标字符串的末尾
		while (*pmove != '\0') {
			pmove++;
		}

		// 将源字符串的最多n个字符拼接到目标字符串的末尾
		while (*source != '\0' && num > 0) {
			*pmove = *source;
			pmove++;
			source++;
			num--;
		}

		*pmove = '\0';
	}
	return destination;
}

int main()
{
	char pa[20] = { 0 };
	//char pb[20] = { 0 };
	scanf("%s", pa);
	//scanf("%s", pb);
	int len = (int)strlen(pa);
	my_strncat(pa, pa, sizeof(char) * (len + 1));
	puts(pa);
	return 0;
}

 //strtok的使用
int main()
{
	char pa[20] = "I am a student!";
	const char* pb = " ";
	char temp[20] = { 0 };
	char* str = NULL;
	strcpy(temp, pa);
	for (str = strtok(temp, pb); str != NULL;str = strtok(NULL, pb))
	{
		printf("%s\n", str);
	}
	return 0;
 }

int main()
{
	char pa[20] = "I am a student!";
	const char* pb = " ";
	char temp[20] = { 0 };
	strcpy(temp, pa);
	char* str = strtok(temp, pb);
	while (str != NULL)
	{
		printf("%s\n", str);
		str = strtok(NULL, pb);
	}
	return 0;
}

 //模拟实现strtok

char* my_strtok(char* str, const char* delimiters)
{
	static char* token = NULL;
	char* start = NULL;
	if (str != NULL) // 第一次调用strtok
	{
		token = str;
	}
	 跳过开头是分隔符的字符
	while (*token != '\0' && strchr(delimiters, *token) != NULL)
	{
		token++;
	}
	 如果token已经达到字符串末尾，直接返回NULL
	if (*token == '\0')
		return NULL;
	 记录分隔符前字符串的起始位置
	start = token;
	 找到下一个分隔符的位置并将分隔符替换为\0
	token = strpbrk(start, delimiters);
	if (token != NULL)
	{
		*token = '\0';
		token++;
	}
	return start; // 返回分隔符前字符串的起始位置
}
int main()
{
	char pa[20] = "I  am a student!";
	const char* pb = " ";
	char temp[20] = { 0 };
	char* str = NULL;
	strcpy(temp, pa);
	for (str = my_strtok(temp, pb); str != NULL; str = my_strtok(NULL, pb))
	{
		printf("%s\n", str);
	}
	return 0;
}
  // 模拟实现strstr函数
char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	int len = (int)strlen(str2);
	while (*str1 != '\0')
	{
		int count = 0;
		int i = 0;
		for (i = 0; i < len; i++)
		{
			if (*(str1 + i) == *(str2 + i))
				count++;
			else
				break;
		}
		if (count == len)
			return str1;
		else
			str1++;
	}
	return NULL;
}
int main()
{
	char pa[20] = { 0 };
	char pb[20] = { 0 };
	scanf("%s", pa);
	scanf("%s", pb);
	char* ret = my_strstr(pa, pb);
	if (ret != NULL)
		puts(ret);
	return 0;
}